package com.neuedu.api.usestring;

import com.neuedu.utils.InputUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述  验证使用String
 * @data 2024/10/2211:10
 */
public class MyString {


    public static void main(String[] args) throws UnsupportedEncodingException {
        //对象的创建
        byte buffer1[] = new byte[]{97,98,99};
        String str1 = new String("abcdef");
        String str2 = new String(buffer1);

        //缓冲区
        byte buffer[] = new byte[1024];
        buffer[0] = 97;
        buffer[1] = 98;
        buffer[2] = 99;
        // offset  length
        String str3 = new String(buffer,0,3);// - abc                                                                                        -
        String str4 = new String(buffer1,"UTF-8");// - abc                                                                                        -
        String str4_1 = new String(buffer1, Charset.forName("utf-8"));// - abc                                                                                        -


        //从字面量获取编码的值  ,默认的编码是 UTF8

        String originStr = "我是一句中文";
        byte[] bytes1 = originStr.getBytes();
        byte[] bytes2 = originStr.getBytes("GBK");
        byte[] bytes3 = originStr.getBytes("utf-8");

        //要求 读取的 转换之后 的编码格式需要一直，否则可能会出现乱码
        String str5 = new String(bytes3,"GBK");

        //使用字面量直接赋值
        String  str6 = "hello idea";
        String  str7 = "hello idea";
        String  str8 = new String("hello idea");



        System.out.println("str1 = " + str1);
        System.out.println("str2 = " + str2);
        System.out.println("str3 = " + str3);
        System.out.println("str4 = " + str4);
        System.out.println("str5 = " + str5);
        System.out.println("str6 = " + str6);
        System.out.println("str6 ==str7 " + (str6 == str7 ));
        System.out.println("str7 ==str8 " + (str7 ==str8 ));


        //字符串的比较使用的是 equals
        System.out.println("str7.equals(str8) = " + str7.equals(str8));
        String str9 = "hello equals";
        String str10  = null;


        //变量 equals 另外一个值  有可能报空指针
        // System.out.println("str10.equals(str9) = " + str10.equals(str9));
        System.out.println("str9.equals(str10) = " + str9.equals(str10));


        //String的不可修改的特性
        String  str11 = "abc";
        System.out.println("str11 = " + str11);
        str11 = "def";
        System.out.println("str11 = " + str11);


        //常用的方法
        String str12  = "abcdef";
        String str13  = "abcDef";
        System.out.println("str12.equals(str13) = " + str12.equals(str13)); //false
        //忽略大小写的比较
        System.out.println("str12.equals(str13) = " + str12.equalsIgnoreCase(str13));  //true



        //比较字符串多大小
        System.out.println("\"abc\".compareTo(\"abd\") = " + "abc".compareTo("abd"));

        System.out.println("\"a\".compareTo(\"b\") = " + "a".compareTo("b"));   //-1
        System.out.println("\"b\".compareTo(\"a\") = " + "b".compareTo("a"));   //1
        System.out.println("\"a\".compareTo(\"a\") = " + "a".compareTo("a"));  //0

        //boolean　startsWith(String　prefix)	测试此字符串是否以指定的前缀开始
        // boolean　endsWith(String　suffix)	测试此字符串是否以指定的后缀结束
        String  str14 = "hello world";
        System.out.println("str14.startsWith(\"hello\") = " + str14.startsWith("hello"));
        System.out.println("str14.endsWith(\"world\") = " + str14.endsWith("world"));



        //常用的方法  lenggth()获取字符串中的字符的个数。
        // String strInput =  InputUtils.getString("请输入一句话");
        // // // System.out.println("您输入的字符个数为:  " + strInput.length());
        // // // System.out.println("hello String.length" + ("hello String".length()));
        // //
        // // //返回指定索引处的　char　值
        // for (int i = 0; i < strInput.length(); i++) {
        //     System.out.println("strInput.charAt(i) = " + strInput.charAt(i));
        // }


        //转换大小写
        //a 97 b 98 c 99      z : 122
        //A 65 B 66 C 67      Z 90:
        //  a-A = 97-65 = 32
        //  b-b = 98-66 = 32
        //......           32
        System.out.println("(int)'a': "+(int)'a');
        System.out.println("(int)'A': "+(int)'A');
        System.out.println("(int)'z': "+(int)'z');
        System.out.println("(int)'Z': "+(int)'Z');

        String originString =  "hello";
        // char charBuffer[] = new char[originString.length()];
        // for (int i = 0; i <originString.length(); i++) {
        //     // charBuffer[0]  = 'h' - 32;
        //     // charBuffer[1]  = 'e' - 32;
        //     charBuffer[i]  =(char)(originString.charAt(i)- 32);
        // }
        // String bigCharStr = new String(charBuffer);
        // System.out.println("bigCharStr = " + bigCharStr);
        System.out.println("originString = " + originString);
        System.out.println("originString.toUpperCase() = " + originString.toUpperCase());
        System.out.println("originString.toUpperCase() = " + originString.toLowerCase());


        // System.out.println("abc".equals(str8));

        // str8.equals("abc")

        System.out.println("===================================");
        System.out.println("===================================");
        System.out.println("===================================");
        System.out.println("===================================");

        System.out.println("\"abcdef\".substring(0,3) = " + "abcdef".substring(0, 3));// [start ,end)
        System.out.println("\"abcdef\".substring(2,3) = " + "abcdef".substring(2, 3));// [start ,end)
        System.out.println("\"abcdef\".substring(0) = " + "abcdef".substring(0));// [0 ,length)
        System.out.println("\"abcdef\".substring(2) = " + "abcdef".substring(2));// [2 ,length)


        System.out.println("\"hello my name is:jshand,age:18\".indexOf(\":\") = " + "hello my name is:jshand,age:18".indexOf(":"));
        System.out.println("\"hello my name is:jshand,age:18\".lastIndexOf(\":\") = " + "hello my name is:jshand,age:18".lastIndexOf(":"));


        String info = "hello my name is:jshand,age:18";

        System.out.println("info = " + info);
        //replace产生了一个新的字符串
        System.out.println("info.replace(\"jshand\",\"金山\") = " + info.replace("jshand", "金山"));
        System.out.println("info.replace(\"l\",\"---\") = " + info.replace("l", "---"));

        int startNameIndex = info.indexOf(":");
        int endNameIndex = info.indexOf(",");
        String name = info.substring(startNameIndex+1, endNameIndex);
        String age =  info.substring(info.lastIndexOf(":")+1);
        System.out.println("name = " + name);
        System.out.println("age = " + age);









    }








}
